package com.maibow.service.admin;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundValueOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import com.maibow.constants.DictionaryCachedKey;
import com.maibow.dao.DaoSupport;
import com.maibow.entity.Page;
import com.maibow.entity.admin.Dictionary;
import com.maibow.entity.admin.DictionaryCatalog;
import com.maibow.exception.BusinessException;
import com.maibow.util.PageData;

/**
 * 
 * 脉博网络后台管理系统
 * 包名：com.maibow.service.system
 * 文件名：DictionariesService.java
 * 作者: 吴比(Wubi)
 * 时间：2015年4月2日-上午9:11:39
 * Copyright@ 2015湖南脉博网络科技有限公司-版权所有
 *
 */
@Service("dictionaryService")
public class DictionaryService {

    @Resource(name = "daoSupport")
    private DaoSupport dao;

    @Autowired(required = false)
    private RedisTemplate redisTemplate;

    /**
     * 
     * queryAllCatalog(这里用一句话描述这个方法的作用)
     * (这里描述这个方法适用条件 – 可选)
     * 标记：@param page
     * 标记：@return
     * 标记：@throws Exception
     * 返回值：List<DictionaryCatalog>
     * 作者：吴比(Wubi)
     * 
     * @exception
     * @since 1.0.0
     */
    @SuppressWarnings("unchecked")
    public List<DictionaryCatalog> queryAllCataloglistPage(Page page) throws Exception {
        return (List<DictionaryCatalog>) dao.findForList("DictionaryMapper.queryAllCataloglistPage", page);
    }

    @SuppressWarnings("unchecked")
    public List<Dictionary> queryAllDictionarylistPage(Page page) throws Exception {
        return (List<Dictionary>) dao.findForList("DictionaryMapper.queryAllDictionarylistPage", page);
    }

    /**
     * 
     * saveDictionaryCatalog(这里用一句话描述这个方法的作用)
     * (这里描述这个方法适用条件 – 可选)
     * 标记：@param dictionaryCatalog
     * 标记：@throws Exception
     * 返回值：void
     * 作者：吴比(Wubi)
     * 
     * @exception
     * @since 1.0.0
     */
    public void saveDictionaryCatalog(DictionaryCatalog dictionaryCatalog) throws Exception {
        dao.save("DictionaryMapper.saveDictionaryCatalog", dictionaryCatalog);
    }

    /**
     * 
     * saveDictionary(这里用一句话描述这个方法的作用)
     * (这里描述这个方法适用条件 – 可选)
     * 标记：@param dictionary
     * 标记：@throws Exception
     * 返回值：void
     * 作者：吴比(Wubi)
     * 
     * @exception
     * @since 1.0.0
     */
    public void saveDictionary(Dictionary dictionary) throws Exception {
        dao.save("DictionaryMapper.saveDictionary", dictionary);
    }

    /**
     * 
     * findCataCodeCount(这里用一句话描述这个方法的作用)
     * (这里描述这个方法适用条件 – 可选)
     * 标记：@param pd
     * 标记：@return
     * 标记：@throws Exception
     * 返回值：PageData
     * 作者：吴比(Wubi)
     * 
     * @exception
     * @since 1.0.0
     */
    public PageData findCataCodeCount(PageData pageData) throws Exception {
        return (PageData) dao.findForObject("DictionaryMapper.findCataCodeCount", pageData);
    }

    /**
     * 
     * findDicCodeCount(这里用一句话描述这个方法的作用)
     * (这里描述这个方法适用条件 – 可选)
     * 标记：@param pageData
     * 标记：@return
     * 标记：@throws Exception
     * 返回值：PageData
     * 作者：吴比(Wubi)
     * 
     * @exception
     * @since 1.0.0
     */
    public PageData findDicCodeCount(PageData pageData) throws Exception {
        return (PageData) dao.findForObject("DictionaryMapper.findDicCodeCount", pageData);
    }

    /**
     * 通过id获取数据
     * findDictionaryCatalogById(这里用一句话描述这个方法的作用)
     * (这里描述这个方法适用条件 – 可选)
     * 标记：@param cataid
     * 标记：@return
     * 标记：@throws Exception
     * 返回值：DictionaryCatalog
     * 作者：吴比(Wubi)
     * 
     * @exception
     * @since 1.0.0
     */
    public DictionaryCatalog findDictionaryCatalogById(Integer cataid) throws Exception {
        return (DictionaryCatalog) dao.findForObject("DictionaryMapper.findDictionaryCatalogById", cataid);
    }

    /**
     * 
     * findDictionaryById(这里用一句话描述这个方法的作用)
     * (这里描述这个方法适用条件 – 可选)
     * 标记：@param dicid
     * 标记：@return
     * 标记：@throws Exception
     * 返回值：Dictionary
     * 作者：吴比(Wubi)
     * 
     * @exception
     * @since 1.0.0
     */
    public Dictionary findDictionaryById(Integer dicid) throws Exception {
        return (Dictionary) dao.findForObject("DictionaryMapper.findDictionaryById", dicid);
    }

    /**
     * 
     * editDictionaryCatalog(这里用一句话描述这个方法的作用)
     * (这里描述这个方法适用条件 – 可选)
     * 标记：@param dictionaryCatalog
     * 标记：@throws Exception
     * 返回值：void
     * 作者：吴比(Wubi)
     * 
     * @exception
     * @since 1.0.0
     */
    public void editDictionaryCatalog(DictionaryCatalog dictionaryCatalog) throws Exception {
        dao.update("DictionaryMapper.editDictionaryCatalog", dictionaryCatalog);
    }

    /**
     * 
     * editDictionary(这里用一句话描述这个方法的作用)
     * (这里描述这个方法适用条件 – 可选)
     * 标记：@param dictionary
     * 标记：@throws Exception
     * 返回值：void
     * 作者：吴比(Wubi)
     * 
     * @exception
     * @since 1.0.0
     */
    public void editDictionary(Dictionary dictionary) throws Exception {
        dao.update("DictionaryMapper.editDictionary", dictionary);
    }

    /**
     * 
     * deleteDictionaryCatalog(这里用一句话描述这个方法的作用)
     * (这里描述这个方法适用条件 – 可选)
     * 标记：@param cataid
     * 标记：@throws Exception
     * 返回值：void
     * 作者：吴比(Wubi)
     * 
     * @exception
     * @since 1.0.0
     */
    public void deleteDictionaryCatalog(Integer cataid) throws Exception {
        dao.delete("DictionaryMapper.deleteDictionaryCatalog", cataid);
    }

    /**
     * 
     * deleteDictionary(这里用一句话描述这个方法的作用)
     * (这里描述这个方法适用条件 – 可选)
     * 标记：@param dicid
     * 标记：@throws Exception
     * 返回值：void
     * 作者：吴比(Wubi)
     * 
     * @exception
     * @since 1.0.0
     */
    public void deleteDictionary(Integer dicid) throws Exception {
        dao.delete("DictionaryMapper.deleteDictionary", dicid);
    }

    /**
     * 
     * findDictionaryByCataCodeCount(
     * 这里用一句话描述这个方法的作用)
     * (这里描述这个方法适用条件 – 可选)
     * 标记：@param pageData
     * 标记：@return
     * 标记：@throws Exception
     * 返回值：PageData
     * 作者：吴比(Wubi)
     * 
     * @exception
     * @since 1.0.0
     */
    public PageData findDictionaryByCataCodeCount(PageData pageData) throws Exception {
        return (PageData) dao.findForObject("DictionaryMapper.findDictionaryByCataCodeCount", pageData);
    }

    /**
     * 
     * queryDictionaryByCodeAndValue(
     * 这里用一句话描述这个方法的作用)
     * (这里描述这个方法适用条件 – 可选)
     * 标记：@param catacode
     * 标记：@return
     * 标记：@throws Exception
     * 返回值：Dictionary
     * 作者：吴比(Wubi)
     * 
     * @exception
     * @since 1.0.0
     */
    public Dictionary queryDictionaryByCodeAndValue(String catacode, String dicvalue) throws Exception {
//        redisTemplate.delete(DictionaryCachedKey.DICTIONARY_CATEGORY_CODE_PREFIX_KEY + catacode + "." + dicvalue);
        BoundValueOperations<String, Dictionary> boundValueOperation = redisTemplate.boundValueOps(DictionaryCachedKey.DICTIONARY_CATEGORY_CODE_PREFIX_KEY + catacode + "." + dicvalue);
        Dictionary dictionary = boundValueOperation.get();
        if (dictionary == null || dictionary.getDiccode() == null) {
            BoundValueOperations<String, List<Dictionary>> boundValueOperations = redisTemplate.boundValueOps(DictionaryCachedKey.DICTIONARY_CATEGORY_CODE_PREFIX_KEY + catacode);
            redisTemplate.delete(DictionaryCachedKey.DICTIONARY_CATEGORY_CODE_PREFIX_KEY + catacode);
            List<Dictionary> dictionaries = boundValueOperations.get();
            if (dictionaries == null || dictionaries.size() == 0 || dictionaries.get(0).getDiccode() == null) {
                try {
                    dictionaries = (List<Dictionary>) dao.findForList("DictionaryMapper.queryAllDictionarylist", catacode);
                    boundValueOperations.set(dictionaries);
                } catch (Exception e) {
                    // TODO Auto-generated catch
                    // block
                    e.printStackTrace();
                }
            }
            if (null != dictionaries) {
                for (Dictionary dic : dictionaries) {
                    if (dicvalue.equals(dic.getDicvalue())) {
                        dictionary = dic;
                        boundValueOperation.set(dic);
                        break;
                    }
                }
            }
        }
        return dictionary;
    }

    /**
     * 
     * getDictionaryListByCode(这里用一句话描述这个方法的作用)
     * (这里描述这个方法适用条件 – 可选)
     * 标记：@param catacode
     * 标记：@return
     * 标记：@throws BusinessException
     * 返回值：List<Dictionary>
     * 作者：吴比(Wubi)
     * 
     * @exception
     * @since 1.0.0
     */
    public List<Dictionary> getDictionaryListByCode(String catacode) throws BusinessException {
        BoundValueOperations<String, List<Dictionary>> boundValueOperations = redisTemplate.boundValueOps(DictionaryCachedKey.DICTIONARY_CATEGORY_CODE_PREFIX_KEY + catacode);
        List<Dictionary> dictionaries = boundValueOperations.get();
        if (dictionaries == null || dictionaries.size() == 0 || dictionaries.get(0).getDiccode() == null) {
            try {
                dictionaries = (List<Dictionary>) dao.findForList("DictionaryMapper.queryAllDictionarylist", catacode);
            } catch (Exception e) {
                // TODO Auto-generated catch
                // block
                e.printStackTrace();
            }

            if (null != dictionaries) {
                boundValueOperations.set(dictionaries);
            }
        }

        return dictionaries;
    }
}
